package control
import(
	"fmt"
	"project06/common/message"
	"net"
	"encoding/json"
	"encoding/binary"
	"project06/client/utils"
)
type UserControl struct{
	
}
func (this *UserControl)Register(userId int,userPwd string,userName string)(err error){
	var Message message.Message
	Message.Type=message.RegisterMesType
	var RegisterMes message.RegisterMes
	conn,err:=net.Dial("tcp","localhost:8999")
	if err!=nil{
		fmt.Println("net.Dial err",err)
		return
	}
	defer conn.Close()

	RegisterMes.User.UserId=userId
	RegisterMes.User.UserPwd=userPwd
	RegisterMes.User.UserName=userName

	data,err:=json.Marshal(RegisterMes)
	if err!=nil{
		fmt.Println("序列化RegisterMes失败，err",err)
		return
	}
	Message.Data=string(data)
	mes,err:=json.Marshal(Message)
	if err!=nil{
		fmt.Println("序列化Message失败，err",err)
		return
	}

	tf:=&utils.Transfer{
		Conn:conn,
	}

	err=tf.WritePkg(mes)
	if err!=nil{
		fmt.Println("发送Message失败，err",err)
		return
	}

	mesRes,err:=tf.ReadPkg()
	if err!=nil{
		fmt.Println("readPak失败，err",err)
		return
	}

	var RegisterResMes message.RegisterResMes
	err=json.Unmarshal([]byte(mesRes.Data),&RegisterResMes)
	if err!=nil{
		fmt.Println("反序列化mes失败，err",err)
		return
	}
	if RegisterResMes.Code==200{
		fmt.Println("注册成功，请退出重新登录")
	}else{
		fmt.Println(RegisterResMes.Error)
	}
	
	return
}
func (this *UserControl)Login(userId int,userPwd string) (err error){
	
	var Message message.Message
	Message.Type=message.LoginMesType
	var LoginMes message.LoginMes
	conn,err:=net.Dial("tcp","localhost:8999")
	if err!=nil{
		fmt.Println("net.Dial err",err)
		return
	}
	defer conn.Close()
	
	LoginMes.UserId=userId
	LoginMes.UserPwd=userPwd

	data,err:=json.Marshal(LoginMes)
	if err!=nil{
		fmt.Println("序列化LoginMes失败，err",err)
		return
	}
	Message.Data=string(data)
	mes,err:=json.Marshal(Message)
	if err!=nil{
		fmt.Println("序列化Message失败，err",err)
		return
	}
	//计算mes的长度；但是因为长度是int类型；但是需要发送[]byte，需要使用binary
	mesLen:=uint32(len(mes))
	var buf [4]byte
	binary.BigEndian.PutUint32(buf[0:4],mesLen)
	//发送mes的长度给服务器
	n,err:=conn.Write(buf[:4])
	if n!=4 || err!=nil{
		fmt.Println("conn.write(buf) err",err)
		return
	}
	//fmt.Println("序列化后的mes输出来看看：",mes)
	n,err=conn.Write(mes)
	if err!=nil{
		fmt.Println("conn.write(mes) err",err)
		return
	}
	//fmt.Printf("给服务器发送了%v个消息\n",n)

	//从服务器接收消息
	tf:=&utils.Transfer{
		Conn:conn,
	}
	mesRes,err:=tf.ReadPkg()
	if err!=nil{
		fmt.Println("readPak失败，err",err)
		return
	}
	//fmt.Println("从服务器接收到的消息mesRes",mesRes)
	var loginResMes message.LoginResMes
	err=json.Unmarshal([]byte(mesRes.Data),&loginResMes)
	if err!=nil{
		fmt.Println("反序列化mes失败，err",err)
		return
	}
	if loginResMes.Code==200{
		curUser.Conn=conn
		curUser.UserId=userId
		fmt.Println("当前在线用户列表：")
		for _,v:=range loginResMes.UsersId{
			if v==userId{
				continue
			}
			fmt.Println("在线用户ID:\t",v)
			user:=&message.User{UserId:v}
			onlineUserMap[v]=user
		}
		fmt.Print("\n\n")
		go keepConnectServer(conn)
		for{
			ShowMenu(userId)
		}
	}else{
		fmt.Println(loginResMes.Error)
	}
	
	return
}


